﻿using System.Reflection;

namespace Sgr.Apis
{
    public static class ValuesEndpoints
    {
        /// <summary>
        /// 注册API基础信息 Minimal API 路由
        /// </summary>
        public static IEndpointRouteBuilder MapValuesEndpoints(this IEndpointRouteBuilder endpoints)
        {
            // 从 DI 获取环境信息，自动判断
            var env = endpoints.ServiceProvider.GetRequiredService<IWebHostEnvironment>();

            endpoints.MapGet("/api/values", (HttpContext context) =>
            {
                var assembly = Assembly.GetExecutingAssembly();
                var version = assembly.GetName().Version?.ToString() ?? "未知版本";
                var infoList = new List<string>
                {
                    "SGR Web API Service",
                    $"Version: {version}"
                };

                // 仅在开发环境暴露文档链接
                if (env.IsDevelopment())
                {
                    var docUrl = $"{context.Request.Scheme}://{context.Request.Host}/docs";
                    infoList.Add($"Documentation: {docUrl}");
                }

                return Results.Ok(infoList);
            })
            .WithTags("System")
            .WithName("GetApiInfo")
            .Produces<IEnumerable<string>>(StatusCodes.Status200OK)
            .WithSummary("获取API基础信息")
            .WithDescription("返回API基本信息，包括：API描述、当前版本、API文档地址（仅开发环境）");

            return endpoints;
        }
    }
}